package com.neusoft.databus.core.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.JsonUtils;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.databus.common.dto.GatewayDTO;
import com.neusoft.databus.common.enums.PhysicalConnType;
import com.neusoft.databus.common.enums.ResourceWorkMode;
import com.neusoft.databus.common.model.Gateway;
import com.neusoft.databus.core.repository.GatewayRepository;
import com.neusoft.databus.core.vo.GatewayPortVO;

/**
 * 网关转换器
 *
 * @author sunchf
 * @date 2018年12月24日 上午9:44:01
 */
@Component
public class GatewayConverter extends AbstractConverter<Gateway, GatewayDTO> {

    @Autowired
    private GatewayRepository gatewayRepository;
    @Autowired
    private BaseIdGenerator gatewayCodeGenerator;

    /**
     * 禁用标识：单独API维护 <br>
     * 关联资源：由资源方维护 <br>
     * 编码：不允许更新
     */
    @Override
    public Gateway toModel(final GatewayDTO dto) {
        Gateway model;
        if (dto.isNew()) {
            model = new Gateway();
            model.setCode(this.gatewayCodeGenerator.generateCode());
        } else {
            model = this.gatewayRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setName(dto.getName());
        model.setPhysicalConnType(dto.getPhysicalConnType());
        model.setConfig(dto.getConfig());
        model.setWorkMode(dto.getWorkMode());
        model.setConnectTimeout(dto.getConnectTimeout());
        model.setConnectRetry(dto.getConnectRetry());
        model.setComment(dto.getComment());
        model.setBatched(dto.isBatched());
        model.setNode(dto.getNode());
        return model;
    }

    @Override
    public GatewayDTO toDTO(final Gateway model, final boolean forListView) {
        final GatewayDTO dto = new GatewayDTO();
        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setDisabled(model.isDisabled());
        dto.setPhysicalConnType(model.getPhysicalConnType());
        dto.setWorkMode(model.getWorkMode());
        dto.setConfig(model.getConfig());
        dto.setConnectTimeout(model.getConnectTimeout());
        dto.setConnectRetry(model.getConnectRetry());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        // 返回关联资源的编码列表
        // model.getResources().stream().map(it ->
        // it.getCode()).forEach(dto.getResources()::add);
        dto.setResourceSize(model.getResources().size());
        dto.setComment(model.getComment());
        dto.setBatched(model.isBatched());
        dto.setNode(model.getNode());
        return dto;
    }

    public Gateway toModel(final GatewayPortVO vo) {
        final Gateway model = new Gateway();
        model.setCode(this.gatewayCodeGenerator.generateCode());
        model.setName(vo.getName());
        model.setPhysicalConnType(PhysicalConnType.valueOf(vo.getPhysicalConnType().toUpperCase()));
        model.setConfig(vo.getConfig());
        JsonUtils.jsonToMap(vo.getConfig());
        model.setWorkMode(ResourceWorkMode.valueOf(vo.getWorkMode().toUpperCase()));
        if (vo.getConnectTimeout() >= 0) {
            model.setConnectTimeout(vo.getConnectTimeout());
        }
        if (vo.getConnectRetry() >= 0) {
            model.setConnectRetry(vo.getConnectRetry());
        }
        model.setDisabled(true);//网关导入默认关闭
        model.setBatched(false);
        model.setNode(0);
        return model;
    }

    public GatewayPortVO toVO(final Gateway model) {
        final GatewayPortVO vo = new GatewayPortVO();
        vo.setCode(model.getCode());
        vo.setName(model.getName());
        vo.setPhysicalConnType(model.getPhysicalConnType().toString().toLowerCase());
        vo.setConfig(model.getConfig());
        vo.setWorkMode(model.getWorkMode().toString().toLowerCase());
        vo.setConnectTimeout(model.getConnectTimeout());
        vo.setConnectRetry(model.getConnectRetry());
        return vo;
    }
}
